package com.andaily.domain.thing;

import com.andaily.domain.AbstractDomain;
import com.andaily.domain.user.User;
import com.andaily.web.context.BeanProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Shengzhao Li
 */
public class Event extends AbstractDomain {


    private static ThingRepository thingRepository() {
        return BeanProvider.getBean(ThingRepository.class);
    }

    private String name;
    private String description;
    private User creator;
    private List<Thing> things = new ArrayList<Thing>();

    public Event() {
    }

    public Event(String name, String description, User creator) {
        this.name = name;
        this.description = description;
        this.creator = creator;
    }

    public String name() {
        return name;
    }

    public String description() {
        return description;
    }

    public User creator() {
        return creator;
    }

    public List<Thing> things() {
        return things;
    }

    public Event update(String name, String description) {
        this.name = name;
        this.description = description;
        return this;
    }

    public Event updateCreator(User creator) {
        this.creator = creator;
        return this;
    }

    public void saveOrUpdate() {
        if (isNewly()) {
            thingRepository().saveEvent(this);
        } else {
            thingRepository().updateEvent(this);
        }
    }
}
